package com.dz.control;

import com.alibaba.fastjson.JSONObject;
import com.dz.bean.guanli;
import com.dz.dao.guanliMapper;
import com.dz.service.DL;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class login {
    @Resource
    private guanliMapper guanliMapper;

    @RequestMapping("/islogin")
//    @ResponseBody
    public String islogin(HttpServletRequest request, HttpServletResponse response, Model model) {
//        //获取session
//        HttpSession session   =   request.getSession();
//// 获取session中所有的键值
//        Enumeration<String> attrs = session.getAttributeNames();
//// 遍历attrs中的
//        while(attrs.hasMoreElements()){
//// 获取session键值
//            String name = attrs.nextElement().toString();
//            // 根据键值取session中的值
//            Object vakue = session.getAttribute(name);
//            // 打印结果
//            System.out.println("------" + name + ":" + vakue +"--------\n");}
//        return "ok";
//        loginService loginService=new loginService();


        String us = request.getParameter("username");
        String ps = request.getParameter("password");
        System.out.println("账号" + us + "密码" + ps);
        request.getSession().setAttribute("us", us);
        request.getSession().setAttribute("ps", ps);
        model.addAttribute("message",us);


//        guanli g1 = new guanli();
//        g1.setAname(us);
//        g1.setApsw(ps);
//        封装用户的登录数据

        try {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(us, ps);
            subject.login(token);
//            唯一性验证
            DL dl=DL.getDl();
            Boolean b=dl.wo(us);
            if(b){
//                guanli g1 = new guanli();
                guanli g1=new guanli();
                g1.setAname(us);
                g1.setApsw(ps);
//                dl.adduser(g1);



//                List<guanli> list = guanliMapper.login(g1);
//                Object o = JSONObject.toJSON(list);

                return "index";
            }else {
                request.setAttribute("asd", "已有人登录");
//                request.setAttribute();
                return "login";
            }
//            DL dl=DL.getDl();
//            Boolean b=dl.wo(us);

//            if(b){
//                List<guanli> list=guanliMapper.login(g1);
//                if(list==null){
//                    request.setAttribute("asd", "用户名/密码 错误");
//                    return "login";
//                }else {
//                    dl.adduser(g1);
//                    return "index";
//                }
//            }
//            else {
//                request.setAttribute("asd", "用户已经登录");
//                return "login";
//            }


        } catch (UnknownAccountException e) {
            request.setAttribute("asd", "用户名/密码 错误");
            return "login";
        } catch (IncorrectCredentialsException e) {
            request.setAttribute("asd", "密码错误");
            return "login";
        }


//        获取当前用户

//        执行登录方法，如果没有异常就ok了

//        if(null == list || list.size() ==0)
//        {
//            request.setAttribute("asd","密码错误，重新登录或者账号已经登录");
//            request.getSession().setAttribute("user",g1);
//
//            return "login";
//        }
//        else {
//
//            return "index";
//        }

    }
}
